<?php 
$province_id = 0;
$city_id = 0;
$county_id = 0;
$base_id = 0;
$ops_provinces = array();
$ops_citys = array();
$ops_countys = array();
$ops_bases = array();
$ops_levels = array();
$bases = array();

/*$provinces = MtsysProvince::model()->findAll();
$citys = MtsysCity::model()->findAll();
$countys = MtsysCounty::model()->findAll();
$bases = MtsysBase::model()->findAll();*/
if(empty($provinces)){
	echo "<script>alert('没有可选单位,请先创建单位,再添加用户');window.location.replace('index.php?r=unit/admin');</script>";	
}else{
	$ops_provinces = CHtml::listData($provinces,'PROVINCE_ID','PROVINCE_NAME');
	$ops_provinces[0] = "请选择省级";
	$ops_citys[0] = "请选择市级";
	$ops_countys[0] = "请选择区县级";
	$ops_bases[0] = "请选择站点";
	$ops_levels[1] = '省级';
}
if(!empty($citys)){
	$ops_levels[2] = '市级';	
}
if(!empty($countys)){
	$ops_levels[3] = '区县级';
}
if(!empty($bases)){
	$ops_levels[4] = '站点';	
}
?>
<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'mtsys-base-form',
	'enableAjaxValidation'=>false,
	'htmlOptions'=>array(
		'class' => 'form',
	),
)); ?>
<fieldset class="grey inline">
	<legend><?php echo Controller::getActionName($this->action->id); ?></legend>
	
	<div class="notification tip" style="display: block; "> 
    <span class="strong">Tip:</span> <?php echo $form->errorSummary($model); ?>
 <span class="close" title="Dismiss"></span>
    </div>
       
<label>
    <label>所属单位</label>
<?php
echo CHtml::link('选择', '#', array(
   'onclick'=>'$("#dialog-unit").dialog("open"); return false;',
));
?>
&nbsp;&nbsp;&nbsp;&nbsp;
    <span id="out-unit"></span>
    <input type="hidden" name="select-unit" value=""/>

<?php 
$this->beginWidget('zii.widgets.jui.CJuiDialog', array(
    'id'=>'dialog-unit',
    'options'=>array(
        'title'=>'选择单位',
		'width'=>'450',
		'height'=>'100',
        'autoOpen'=>false,
    ),
));
?>
<div id="template-unit">
<?php
echo CHtml::dropDownList('User[PROVINCE_ID]',$province_id, $ops_provinces ,array(
	'ajax' => array(
		'type'=>'POST',
		'url'=> array('/btBatterys/getUnitCitys'),
		'update'=>'#CITY_ID',
		'data'=>array('PROVINCE_ID'=>'js:$("#PROVINCE_ID").val()'),
	),
	'id'=>'PROVINCE_ID',			
));
echo CHtml::dropDownList('User[CITY_ID]',$city_id, $ops_citys, array(
	'ajax' => array(
		'type'=>'POST',
		'url'=> array('/btBatterys/getUnitCountys'),
		'update'=>'#COUNTY_ID',
		'data'=>array('CITY_ID'=>'js:$("#CITY_ID").val()'),
	),
	'id'=>'CITY_ID',
));
echo CHtml::dropDownList('User[COUNTY_ID]',$county_id, $ops_countys, array(
	'ajax' => array(
		'type'=>'POST',
		'url'=> array('/btBatterys/getUnitBases'),
		'update'=>'#BASE_ID',
		'data'=>array('COUNTY_ID'=>'js:$("#COUNTY_ID").val()'),
	),
	'id'=>'COUNTY_ID',
));
echo CHtml::dropDownList('User[BASE_ID]',$base_id, $ops_bases, array(
	'id'=>'BASE_ID',
));
?>
<input type="button" name="op-select" value="确定" data-type="unit"/>
</div>
<?php
$this->endWidget('zii.widgets.jui.CJuiDialog');
?>
    
</label>
<br />  
  
<label>
<label>制造商</label>
<?php
echo CHtml::link('选择', '#', array(
   'onclick'=>'$("#dialog-maker").dialog("open"); return false;',
));
?>
&nbsp;&nbsp;&nbsp;&nbsp;
<span id="out-maker"></span>
<input type="hidden" name="select-maker" value=""/>

<?php 
$this->beginWidget('zii.widgets.jui.CJuiDialog', array(
    'id'=>'dialog-maker',
    'options'=>array(
        'title'=>'选择制造商',
		'width'=>'250',
		'height'=>'100',
        'autoOpen'=>false,
    ),
));
?>
<div id="template-maker">
<?php 
	$optionss = array();	
    if(!empty($manufaturer)){
    	$optionss = CHtml::listData($manufaturer,'RECORD_ID','MANNAME_V');
    }
    	$optionss[0] = "请选择制造商";
   echo CHtml::dropDownList('DictManufacturer[RECORD_ID]','', $optionss, array(
		'id'=>'RECORD_ID', 	));
?>	
<input type="button" name="op-select" value="确定" data-type="maker"/>
</div>
<?php
$this->endWidget('zii.widgets.jui.CJuiDialog');
?>
</label>
<br />
<label>
<label>电池规格</label>
<?php
echo CHtml::link('选择', '#', array(
   'onclick'=>'$("#dialog-size").dialog("open"); return false;',
));
?>
&nbsp;&nbsp;&nbsp;&nbsp;
<span id="out-size"></span>
<input type="hidden" name="select-size" value=""/>

<?php 
$this->beginWidget('zii.widgets.jui.CJuiDialog', array(
    'id'=>'dialog-size',
    'options'=>array(
        'title'=>'选择规格',
		'width'=>'250',
		'height'=>'100',
        'autoOpen'=>false,
    ),
));
?>
<div id="template-size">
<?php  
$optionss = array();
if(!empty($manufaturer)){
	$optionss = CHtml::listData($dictbattery,'RECORD_ID','BATTERYTYPE_V');
}
$optionss[0] = "请选择电池规格";
echo CHtml::dropDownList('DictBattery[RECORD_ID]','', $optionss, array(
		'id'=>'RECORD_ID', 	));
?>
<input type="button" name="op-select" value="确定" data-type="size"/>
</div>
<?php
$this->endWidget('zii.widgets.jui.CJuiDialog');
?>
</label>
<br />	
<label>
	<label>电池组创建年份</label>
<?php
echo CHtml::link('选择', '#', array(
   'onclick'=>'$("#dialog-timer").dialog("open"); return false;',
));
?>
&nbsp;&nbsp;&nbsp;&nbsp;
<span id="out-timer"></span>
<input type="hidden" name="select-timer" value=""/>

<?php 
$this->beginWidget('zii.widgets.jui.CJuiDialog', array(
    'id'=>'dialog-timer',
    'options'=>array(
        'title'=>'选择年份',
		'width'=>'300',
		'height'=>'100',
        'autoOpen'=>false,
    ),
));
?>
<div id="template-timer">
<?php 
$years = array();
$optionss = array();
if(!empty($manufaturer)){
	$years = CHtml::listData($batterys,'RECORD_ID','SETUP_D');
}
if(!empty($years)){
	foreach($years as $val => $name){
		$name = substr($name, 0, 4);		
		$optionss[$name] = $name;	
	}	
	$optionss = array_unique($optionss);
	//sort($optionss);
}

$optionss[0] = "请选择创建年份";

echo CHtml::dropDownList('BtBatterys[RECORD_ID]','', $optionss, array(
		'id'=>'RECORD_ID', 	));
?>
<input type="button" name="op-select" value="确定" data-type="timer"/>
</div>
<?php
$this->endWidget('zii.widgets.jui.CJuiDialog');
?>
</label>

<?php 
/*
echo "开始时间";
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
    'name'=>'MtsysBase[start_time]',
    'value'=>'',
    'options'=>array(
        'showAnim'=>'fold',
		'dateFormat'=>'yy-mm-dd'
    ),
    'htmlOptions'=>array(
        'style'=>'height:20px;'
    ),
	'language'=>'ZH-CN'
));
echo "结束时间";
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
    'name'=>'MtsysBase[end_time]',
    'value'=>'',
    'options'=>array(
        'showAnim'=>'fold',
		'dateFormat'=>'yy-mm-dd'
    ),
    'htmlOptions'=>array(
        'style'=>'height:20px;'
    ),
	'language'=>'ZH-CN'
));
*/
?>

<br />
	<?php echo CHtml::button('详细报告', array('submit' => array('btBatterys/details'),'class'=>'button blue inline','style'=>'width:100px;')); ?>
	<?php echo CHtml::button('图形显示', array('submit' => array(
'btBatterys/charts'), 'class'=>'button blue')); ?>
		<?php echo CHtml::button('数据导出', array('submit' => array('btBatterys/Importdata' ), 
			 'params' => array( 'province_id'=>'MtsysBase[PROVINCE_ID]',
			 'city_id'=>'MtsysBase[CITY_ID]',
			 'county_id'=>'MtsysBase[COUNTY_ID]',
			 'manu_id'=>'DictManufacturer[RECORD_ID]',
			 'dict_id'=>'DictBattery[RECORD_ID]',),
			'class'=>'button blue inline','style'=>'width:100px;')); ?>

		<?php // echo CHtml::submitButton('详细报告', array('submit' => array('btBatterys/details', array('class'=>'button blue'))); ?>
	</fieldset>
<?php $this->endWidget(); ?>

<script>
var Search = {
	isMulti : function(type){
		var unitQuery = $("input[name=select-unit]").val().split('|'),
			makerQuery = $("input[name=select-maker]").val().split('|'),
			sizeQuery = $("input[name=select-size]").val().split('|'),
			timerQuery = $("input[name=select-timer]").val().split('|'),
			isok = false,
			mtype = '';
		
		if(unitQuery.length >= 2){
			mtype = 'unit';
			isok = true;	
		}
		if(makerQuery.length >= 2){
			mtype = 'maker';
			isok = true;
		}
		if(sizeQuery.length >= 2){
			mtype = 'size';
			isok = true;	
		}
		if(timerQuery.length >= 2){
			mtype = 'timer';
			isok = true;	
		}
		if(isok && (mtype == type) || ($("input[name=select-"+ type +"]").val() == '')){
			isok = false;	
		}
		return isok;	
	},
	render : function(type, name){
		var output= $('#out-'+type),
			_html = output.html(), 
			rs = [];
		
		if(_html.indexOf(name) >= 0){
			//alert('重复的选项，请重选');	
		}else{
			if(_html != ''){
				rs.push(_html);	
			}
			if(name != ''){
				rs.push(name);	
			}	
			output.html(rs.join(','));
		}
	},
	
	unit : {		
		init : function(){
			var inputFirst = $('#PROVINCE_ID'),
				fst = inputFirst.val(),
				inputSecond = $('#CITY_ID'),				
				sec = inputSecond.val(), 
				inputThird = $('#COUNTY_ID'),
				trd = inputThird.val(),
				inputFour = $('#BASE_ID'),
				four = inputFour.val(),				
				rs = [], name = [];
			
			if(fst != '0' && fst != '-1'){
				rs.push(fst);
				name.push(inputFirst.find('option:selected').text());
			}
			if(sec != '0' && sec != '-1'){
				rs.push(sec);
				name.push(inputSecond.find('option:selected').text());
			}
			if(trd != '0' && trd != '-1'){
				rs.push(trd);
				name.push(inputThird.find('option:selected').text());
			}
			if(four != '0' && four != '-1'){
				rs.push(four);
				name.push(inputFour.find('option:selected').text());
			}
			return {
				'id': rs.join(','),
				'name' : name.join('-')
			};
		}
	},
	
	other : {
		init : function(type){
			var inputSelect = $('#template-'+ type +' #RECORD_ID'),
				name = inputSelect.find('option:selected').text(),
				value = inputSelect.val();
			/*
			if(!Search.isMulti(type) && (value > 0)){
				Search.render(type, name);	
				return value;
			}
			*/
			return {'id':value,'name':name};
		}	
	}
};

$("input[name=op-select]").each(function(index, element) {
   $(element).bind('click',function(){
		var type = $(this).attr('data-type'),
			output = $('input[name=select-'+ type +']'),
			_rs = output.val(),
			_new = (type != 'unit')?Search['other'].init(type):Search['unit'].init(),
			rs = [], tmp = null, isok = true;
		
		if(_rs != ''){
			tmp = _rs.split('|');
			rs.push(_rs);
		}
		if(tmp != null){						
			$(tmp).each(function(index, _item) {
                if(_item == _new['id']){
					isok = false;
					alert('选择条件已存在，请重新选择');
					return false;
				}
				if(type == 'unit' && (_item.length != _new['id'].length)){
					isok = false;
					alert('格式不对，请与上一次单位同级');
					return false;
				}
            });
		}
		if(Search.isMulti(type)){
			isok = false;
			alert('仅一个条件允许选择2个以上');
			return false;
		}
		if((_new['id'] != '') && (_new['id'] != 0) && isok){
			rs.push(_new['id']);	
			Search.render(type, _new['name']);
		}
		
		output.val(rs.join('|'));
	}); 
});



</script>

